<?php
/**
 * @投融
 * 1投融列表
 * 2投融详情
 */
namespace app\invest\controller;
use app\common\controller\HomeCommon;
use think\Cache;
use think\Config;

class Index extends HomeCommon
{
    public function __construct()
    {

        parent::__construct();

        $arr = [
            ['id' => 1, 'name' => '中国'],
            ['id' => 11, 'name' => '河南'],
            ['id' => 158, 'name' => '三门峡'],
        ];
        //db('invest_organ')->where(['id'=>3])->update(['city_str'=>serialize($arr)]);
    }



	/**
	 * 投融列表
    */
	public function get_invest(){


		$where="  a.status=1  ";
		$act=request()->param('act','event','trim');


		if($act=='event'){
			$field="a.identification,a.event_name,a.turns,a.invest_time,a.invest_money_str as invest_money,a.industry_str as industry,a.city_str as city,
                      a.equity_ratio,a.after_worth,a.after_worth_val,e.company_name,f.organ_name";
			$obj=db('invest_event a')->join('companys e','a.com_id=e.id','left')->join('invest_organ f','a.com_id=f.com_id','left');

	        /****/
			$com_id=request()->param('com_id',0,'intval');
			if(!empty($com_id)){
				$where.=" and a.com_id=$com_id  ";
			}
		}elseif($act=='organ'){

			$field="a.identification,a.organ_name,a.turns,a.ty_invest_nums,a.invest_nums,a.industry_str as industry,a.city_str as city,a.logo";
			$obj=db('invest_organ a');
		}else{
			return json(['errcode'=>'-1','msg'=>lang('param error'),'data'=>[]]);
		}



        /***鍏抽敭瀛�*/
		$keywords=request()->param('keywords','','trim');
		if(!empty($keywords)){
			$where.=" and a.".$act."_name like '%$keywords%' ";
		}
        /***琛屼笟*/
		$industry_id=request()->param('industry_id',0,'intval');
		if(!empty($industry_id)){
			$obj=$obj->join('invest_'.$act.'_industry b','a.id=b.invest_id');
			$where.=" and b.status=1 and b.industry_id=$industry_id";
		}

        /***浜т笟閾�*/
		$chain_id=request()->param('chain_id',0,'intval');
		if(!empty($chain_id)){
			$obj=$obj->join('invest_'.$act.'_chain c','a.id=c.invest_id');
			$where.=" and c.status=1 and c.chain_id=$chain_id";
		}

        /***鍩庡競*/
		$city_id=request()->param('city_id',0,'intval');
		if(!empty($city_id)){
			$where.=" and (a.province_id=$city_id or a.city_id=$city_id or a.area_id=$city_id ) ";
		}
        // 设置分页数据
        $page=request()->param('page',0,'intval');
        $limit=request()->param('limit',15,'intval');
        Config::set('paginate.page',bcadd($page,1));
		$listobj=$obj->field($field)->where($where)->order('a.id desc')->paginate()->toArray($limit);
        //p($listobj);
		if($act=='event'){
		    //var_dump($listobj['data']);die();
			if(count($listobj['data'])>0){
				foreach ($listobj['data'] as &$val){
			     $val['organ']=[
							'identification'=>$val['identification'],
							'organ_name'=>$val['organ_name'],
						];

				}
			}
		}



		foreach ($listobj['data'] as &$val){
			$val['turns']=turns($val['turns']);//投资轮次
			$val['industry']=unserialize($val['industry']);//行业领域
			$val['city']=unserialize($val['city']);//行业领域

		}





		return json(['errcode'=>'0','msg'=>lang('Get success'),'data'=>$listobj]);


	}

	/**
	 * 投融详情
     * */
	public function get_invest_info(){


		$where['status']=['=',1];


		$identification=request()->param('identification','','trim');

		$act=request()->param('act','event','trim');
		if($act=='event'){
			$field="id,identification,event_name,turns,desc,invest_time,invest_money_str as invest_money,value_str as value,industry_str as industry,city_str as city,com_id,equity_ratio,after_worth_val";

		}elseif($act=='organ'){

			$field="id,identification,thumb,organ_name,desc,ty_invest_nums,invest_nums,industry_str as industry,city_str as city,turns";
		}else{
			return json(['errcode'=>'-1','msg'=>lang('param error'),'data'=>[]]);
		}

		if(!empty($identification)){
			$where['identification']=['=', $identification];


			$cache=new Cache();

			$result=$cache->get('invest_'.$act.'_'.$identification);
			if($result&&1==2){
				$info=unserialize($result);

			}else{
				$model= model('Commondata');

				$info=$model->get_detail('invest_'.$act,$field,$where);
				if(empty($info)){
					return json(['errcode'=>'-1','msg'=>lang('param error'),'data'=>[]]);
				}
				$info['industry']=unserialize($info['industry']);//行业领域
				$info['city']=unserialize($info['city']);//行业领域
		    //新闻资讯
				$news_list=db('invest_'.$act.'_news a')->join('news b','a.news_id=b.id')
				->field('b.identification,b.news_name,b.create_time')
				->where(['a.status'=>1,'b.status'=>1,'a.invest_id'=>$info['id']])
				->order('b.id desc')
				->select();
				$info['newslist']=$news_list;

				$info['turns']=turns($info['turns']);

				if($act=='event'){
			//投资方
					$info['company_name']=db('companys')->where(['id'=>$info['com_id']])->value('company_name');


					$investor=db('invest_event_investor a')
					->field('b.company_name,b.logo,a.turns,a.money_str as money,a.type,b.create_time,a.next_turns_ratio,a.invest_count')
					->join('companys b','a.com_id=b.id','left')
					->where(['a.status'=>1,'a.invest_id'=>$info['id']])
					->select();
					foreach ($investor as &$val){
						$val['turns']=turns($val['turns']);
						$val['type']=service_type($val['type']);
					}
					$info['investor']=$investor;

			// 融资金融分析
                    $analyze = db('invest_event_analyze')
                        ->field('create_time,type,turns,count,avg_money_str,mid_money_str,now_money_str')
                        ->where('com_id',$info['com_id'])
                        ->where('identification',$identification)
                        ->select();
                    $info['analyze'] = $analyze;
                    foreach ($info['analyze'] as &$val){
                        $val['turns'] = turns($val['turns']);
                        $val['type'] = service_type($val['type']);
                    }
                    unset($info['com_id']);
				}elseif($act=='organ'){


			//投资历史
					$invest_history=db('invest_organ_ivhistory')
					->field('turns,sum(money) as total,count(id) as num')
					->where(['status'=>1,'invest_id'=>$info['id']])
					->group('turns')
					->select();
					foreach ($invest_history as &$val){
						$val['turns']=turns($val['turns']);
					}
					
				  
					$info['invest_history']=$invest_history;
					
					
					
				}
				
				unset($info['id']);
				
			
			
				
				
				$cache->set('invest_'.$act.'_'.$identification, serialize($info),3600);
			}
			
			
			
			
			
			
			return json(['errcode'=>'0','msg'=>lang('Get success'),'data'=>$info]);
			
			
		}else{
			return json(['errcode'=>'-1','msg'=>lang('param error'),'data'=>[]]);
		}
		
		
	
		
		
		
		
	}
}
